<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>Title</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="" lay-filter="user-edit">
			<div class="mainBox">
				<div class="main-container">
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="role-give">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery','context','permission','popup'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let context = layui.context;
				let popup = layui.popup;
				
				let basePath = context.get("base-path");
				let currentId = parent.layui.jquery("#id").val();

				$.ajax({
					url: basePath + '/api/security/list',
					dataType: 'json',
					contentType: 'application/json',
					type: 'get',
					success: function(result) {
						for (i = 0; i < result.data.items.length; i++) { 
						    var item = result.data.items[i]; 
							$(".main-container")
					.append('<div class="layui-form-item">'+
								'<label class="layui-form-label"></label>'+
								'<div class="layui-input-block">'+
									'<input type="checkbox" name="securitie" title="'+ item.uniqueName +'" value="'+item.id+'" lay-skin="primary">'+
								'</div>'+
							'</div>')
						}
						form.render();
						$.ajax({
							url: basePath + '/api/role/'+ currentId +'/securities',
							dataType: 'json',
							contentType: 'application/json',
							type: 'get',
							success: function(result) {
								for (i = 0; i < result.data.length; i++) {
								    var item = result.data[i]; 
									$("[name='securitie'][value='"+item.id+"']").attr("checked",true);
								}			
								form.render();
							}
						})
					}
				})

				form.on('submit(role-give)', function(data) {
					let securitieIds = "";
					$('input[type=checkbox]:checked').each(function () {
					      securitieIds += $(this).val() + ",";
					});
					securitieIds = securitieIds.substr(0, securitieIds.length - 1);
					var saveIds= new Array();
					saveIds= securitieIds.split(","); 
					$.ajax({
						url: basePath + '/api/security/give/'+currentId,
						data: JSON.stringify(saveIds),
						dataType: 'json',
						contentType: 'application/json',
						type: 'post',
						success: function(result) {
							if(result.succeeded){
							    popup.success("修改成功",function(){
							        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
							        parent.layui.table.reload("role-table");
							    });
							}else{
							    popup.failure("修改失败");
							}
						}
					})
					return false;
				});
			})
		</script>
	</body>
</html>
